/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.wtk.effects;

import wtkx.Component;
import wtkx.wtk.Bounds;
import wtkx.wtk.Point;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

/**
 * Effect that translates the paint origin of its component.
 *
 * @author gbrown
 */
public class TranslationEffect
    extends Object
    implements Effect
{

    private int x = 0;
    private int y = 0;
    private boolean clip = false;


    public TranslationEffect() {
        super();
    }
    public TranslationEffect(boolean clip) {
        super();
        this.setClip(clip);
    }
    public TranslationEffect(int x, int y, boolean clip) {
        super();
        this.setOffset(x, y);
        this.setClip(clip);
    }


    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Point getOffset() {
        return new Point(x, y);
    }
    public void setOffset(Point offset) {
        if (offset == null) {
            throw new IllegalArgumentException("offset is null.");
        }

        setOffset(offset.x, offset.y);
    }
    public void setOffset(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public boolean getClip() {
        return clip;
    }
    public void setClip(boolean clip) {
        this.clip = clip;
    }

    public Graphics2D prepare(Component component, Graphics2D graphics) {
        graphics.translate(x, y);
        return graphics;
    }

    public void update() {
    }

    public Bounds getBounds(Component component) {
        Bounds bounds = new Bounds(x, y, component.getWidth(), component.getHeight());

        if (clip) {
            bounds.intersect(component.getBounds());
        }

        return bounds;
    }

    public AffineTransform getTransform(Component component) {
        return AffineTransform.getTranslateInstance(x, y);
    }
}
